

//
//  MultipleLabelsCellData.m
//  18Birdies
//
//  Created by poleness on 2017/9/15.
//  Copyright © 2017年 Lc. All rights reserved.
//

#import "MultipleLabelsCellData.h"
#import "MultipleLabelsCell.h"
#import "SingleLineLabel.h"

@implementation LabelModel

- (instancetype)initWithTitle:(NSString*)title content:(NSString*)content {
  return [self initWithTitle:title content:content titleFont:LcFont.dr14 contentFont:LcFont.gr14];
}
- (instancetype)initWithTitle:(NSString*)title
                      content:(NSString*)content
                    titleFont:(LcFont*)titleFont
                  contentFont:(LcFont*)contentFont {
  LC_CHECK_TRUE(title.length > 0 || content.length > 0);
  NSAttributedString* attributedTitle =
      [NSAttributedString attributedStringWithString:title lcFont:titleFont];
  NSAttributedString* attributedContent =
      [NSAttributedString attributedStringWithString:content lcFont:contentFont];
  return [self initWithAttributedTitle:attributedTitle attributedContent:attributedContent];
}

- (instancetype)initWithAttributedTitle:(NSAttributedString*)attributedTitle
                      attributedContent:(NSAttributedString*)attributedContent {
  LC_CHECK_TRUE(attributedTitle != nil || attributedContent != nil);
  self = [super init];
  if (self) {
    self.attributedTitle = attributedTitle;
    self.attributedContent = attributedContent;
  }
  return self;
}

@end

@implementation MultipleLabelsCellData

+ (instancetype)cellDataWithLabelModel:(NSArray*)labelModels {
  MultipleLabelsCellData* cellData = [[MultipleLabelsCellData alloc] init];
  cellData.labelModels = labelModels;
  cellData.clickable = NO;
  return cellData;
}
+ (instancetype)cellDataWithTitles:(NSArray*)titles
                          contents:(NSArray*)contents
                         titleFont:(LcFont*)titleFont
                       contentFont:(LcFont*)contentFont {
  LC_CHECK_TRUE(titles.count == contents.count);
  LC_CHECK_TRUE(titles.count > 0);
  NSMutableArray* labelModels = [NSMutableArray arrayWithCapacity:titles.count];
  for (int i = 0; i < titles.count; ++i) {
    LabelModel* labelModel = [[LabelModel alloc] initWithTitle:titles[i]
                                                       content:contents[i]
                                                     titleFont:titleFont
                                                   contentFont:contentFont];
    [labelModels addObject:labelModel];
  }
  return [self cellDataWithLabelModel:labelModels];
}

- (Class)cellClazz {
  return MultipleLabelsCell.class;
}

- (NSString*)nibName {
  return @"MultipleLabelsCell";
}

- (void)refreshCell:(MultipleLabelsCell*)cell {
  [super refreshCell:cell];
  [cell.contentView removeAllSubviews];
  CGFloat totalHeight = 10;
  for (int i = 0; i < self.labelModels.count; ++i) {
    LabelModel* labelModel = self.labelModels[i];
    SingleLineLabel* label =
        [[NSBundle mainBundle] loadNibNamed:@"SingleLineLabel" owner:nil options:nil][0];
    label.autoresizingMask = UIViewAutoresizingNone;
    label.leftLabel.attributedText = labelModel.attributedTitle;
    label.rightLabel.attributedText = labelModel.attributedContent;
    label.backgroundColor = [UIColor clearColor];
    [label layoutIfNeeded];
    CGFloat height = MAX(label.leftLabel.height, label.rightLabel.height);
    label.frame = CGRectMake(0, totalHeight, LcUIUtility.screenWidth, height + 6);
    [cell.contentView addSubview:label];

    totalHeight += label.height;
  }
  // bottom margin
  totalHeight += 8;
  self.cellHeight = self.labelModels.count > 0 ? totalHeight : 0;
}

@end
